package com.soft.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Orders;
import com.soft.mapper.OrdersMapper;

@Service
public class OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    // 查询所有订单
    public List<Orders> queryAll() {
        return ordersMapper.selectList(null);
    }

    // 根据用户ID查询订单
    public List<Orders> queryByUserId(Integer userId) {
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        return ordersMapper.selectList(qw);
    }

    // 根据商品名称模糊查询订单
    public List<Orders> queryByProductName(String productName) {
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        qw.like("product_name", productName);
        return ordersMapper.selectList(qw);
    }

    // 添加新订单
    public int insertOne(Integer userId, String productName, Integer quantity, BigDecimal price) {
        Orders order = new Orders();
        order.setUserId(userId);
        order.setProductName(productName);
        order.setQuantity(quantity);
        order.setPrice(price);
        order.setOderDate(new Date()); // 设置当前时间为订单时间
        return ordersMapper.insert(order);
    }

    // 根据订单ID删除订单
    public int deleteById(Integer orderId) {
        return ordersMapper.deleteById(orderId);
    }

    // 更新订单状态
    public int updateStatus(Integer orderId, String status) {
        Orders order = new Orders();
        order.setOrderId(orderId);
        return ordersMapper.updateById(order);
    }

    // 更新订单价格
    public int updatePrice(Integer orderId, BigDecimal price) {
        UpdateWrapper<Orders> uw = new UpdateWrapper<>();
        uw.eq("order_id", orderId)
          .set("price", price);
        return ordersMapper.update(null, uw);
    }
}